package com.jh.readme.iu.fragment.newslist;

import java.util.ArrayList;
import java.util.List;

import com.jh.readme.R;
import com.jh.readme.iu.activity.ndetail.NewsDetailActivity;
import com.jh.tools.bean.NeteastNewsSummary;
import com.jh.tools.common.ImagUtil;
import com.jh.tools.common.phone.ToActivityUtil;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {

    private List<NeteastNewsSummary> mDatas;
    private Context mContext;
    private LayoutInflater inflater;

    public MyRecyclerAdapter(Context context,List<NeteastNewsSummary> mData){
        this. mContext=context;
        this. mDatas=mData;
        inflater=LayoutInflater. from(mContext);
    }
    @Override
    public int getItemCount() {

        return mDatas.size();
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        ImagUtil.getInstance().getImageLoader(mDatas.get(position).imgsrc,holder.iv_news_summary_photo);

        holder.tv_news_summary_title.setText(mDatas.get(position).title);;
        holder.tv_news_summary_digest.setText(mDatas.get(position).digest);
        holder.tv_news_summary_ptime.setText(mDatas.get(position).ptime);
        holder.home_item_root_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, NewsDetailActivity.class);
                intent.putExtra("newsId",mDatas.get(position).cid);
                intent.putExtra("newsName",mDatas.get(position).ename);
                mContext.startActivity(intent);
            }
        });
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_news_summary,parent, false);
        MyViewHolder holder= new MyViewHolder(view);
        return holder;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_news_summary_title;
        TextView tv_news_summary_digest;
        TextView tv_news_summary_ptime;
        ImageView iv_news_summary_photo;
        CardView home_item_root_view;
        public MyViewHolder(View view) {
            super(view);
            tv_news_summary_title=(TextView) view.findViewById(R.id.tv_news_summary_title);
            tv_news_summary_digest=(TextView) view.findViewById(R.id.tv_news_summary_digest);
            tv_news_summary_ptime=(TextView) view.findViewById(R.id.tv_news_summary_ptime);
            iv_news_summary_photo=(ImageView) view.findViewById(R.id.iv_news_summary_photo);
            home_item_root_view=(CardView) view.findViewById(R.id.home_item_root_view);
        }

    }


}